//
//  HouseTypeInfoMessageView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/25.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "HouseTypeInfoMessageView.h"
#import "Masonry.h"
#import "LabelUtil.h"
#import "UIImageView+FadeWebCache.h"

@interface HouseTypeInfoMessageView()
@property (nonatomic, strong) UIImageView *advertisementImageView;
@property (nonatomic, strong) UILabel *houseLayoutLabel;
@property (nonatomic, strong) UILabel *saleStateLabel;
@property (nonatomic, strong) UILabel *houseAreaLabel;
@property (nonatomic, strong) UILabel *averageLabel;
@property (nonatomic, strong) UILabel *totalLabel;
@property (nonatomic, strong) UILabel *houseTypeLocationLabel;
@property (nonatomic, strong) UIView *houseTagView;
@property (nonatomic, strong) UILabel *houseIntroductionLabel;
@end

@implementation HouseTypeInfoMessageView

#pragma mark - Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadHouseTypeInfoMessageView];
    }
    return self;
}

#pragma mark - Private
- (void)loadHouseTypeInfoMessageView{
    self.translatesAutoresizingMaskIntoConstraints = NO;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    [self.advertisementImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.width.equalTo(self);
        make.height.mas_equalTo(screenWidth*0.75);
    }];
    
    UIView *messageBackView = [UIView new];
    UIView *introductionBackView = [UIView new];
    
    [self addSubview:messageBackView];
    [self addSubview:introductionBackView];
    
    [messageBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.advertisementImageView.mas_bottom);
        make.left.width.equalTo(self);
    }];
    [introductionBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageBackView.mas_bottom);
        make.left.width.equalTo(self);
        make.bottom.equalTo(self).offset(-TweentyOffsetViewMargin);
    }];
    
    [self loadMessageViewWithBackView:messageBackView];
    [self loadIntroductionViewWithBackView:introductionBackView];
    
    UIView *barView = [UIView new];
    barView.backgroundColor = [UIColor colorWithRed:215/255.0 green:215/255.0 blue:215/255.0 alpha:1.0];
    [self addSubview:barView];
    
    [barView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(10);
    }];
}
//加载户型信息的背景view
- (void)loadMessageViewWithBackView:(UIView *)backView{
    UIView *layoutBackView = [UIView new];
    UIView *priceBackView = [UIView new];
    UIView *locationBackView = [UIView new];
    UIView *buttonsBackView = [UIView new];
    
    [backView addSubview:layoutBackView];
    [backView addSubview:priceBackView];
    [backView addSubview:locationBackView];
    [backView addSubview:self.houseTagView];
    [backView addSubview:buttonsBackView];
    
    [layoutBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView).offset(TenOffsetViewMargin);
        make.top.equalTo(backView).offset(TenOffsetViewMargin);
        make.height.mas_equalTo(ScreenHeight*0.07);
    }];
    [priceBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(layoutBackView.mas_bottom).offset(5);
        make.left.equalTo(layoutBackView).offset(FifteenoffsetViewMargin);
        make.height.equalTo(layoutBackView);
    }];
    [locationBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceBackView.mas_bottom).offset(5);
        make.left.equalTo(priceBackView);
        make.height.equalTo(priceBackView);
    }];
    [self.houseTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(locationBackView.mas_bottom).offset(5);
        make.left.equalTo(locationBackView);
        make.bottom.equalTo(backView).offset(-20);
        make.height.equalTo(locationBackView);
    }];
    [buttonsBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView).offset(TenOffsetViewMargin);
        make.right.equalTo(backView).offset(-FifteenoffsetViewMargin);
        make.height.equalTo(backView).multipliedBy(0.3);
        make.width.equalTo(buttonsBackView.height);
    }];
    
    [self loadLayoutViewWithBackView:layoutBackView];
    [self loadPriceViewWithBackView:priceBackView];
    [self loadLocationViewWithBackView:locationBackView];
    [self loadButtonsBackViewWithView:buttonsBackView];
    NSMutableArray *titles = [NSMutableArray arrayWithObjects:@"优惠中",@"中户型",@"学区房",@"低首付", nil];
    NSMutableArray *colors = [NSMutableArray arrayWithObjects:[UIColor colorWithRed:242/255.0 green:78/255.0 blue:82/255.0 alpha:1.0],[UIColor colorWithRed:239/255.0 green:200/255.0 blue:39/255.0 alpha:1.0],[UIColor colorWithRed:72/255.0 green:139/255.0 blue:153/255.0 alpha:1.0],[UIColor colorWithRed:230/255.0 green:153/255.0 blue:130/255.0 alpha:1.0], nil];
    [self loadHouseTagViewWithBackView:self.houseTagView withTagTextArray:titles andTagColorArray:colors];
}

- (void)loadLayoutViewWithBackView:(UIView *)backView{
    [backView addSubview:self.houseLayoutLabel];
    [backView addSubview:self.saleStateLabel];
    [backView addSubview:self.houseAreaLabel];
    
    [self.houseLayoutLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(backView);
    }];
    [self.saleStateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.houseLayoutLabel.mas_right);
        make.top.bottom.equalTo(self.houseLayoutLabel);
    }];
    
    [self.houseAreaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(backView);
        make.right.equalTo(backView);
        make.left.mas_greaterThanOrEqualTo(self.saleStateLabel.mas_right).offset(10);
    }];
}

- (void)loadPriceViewWithBackView:(UIView *)backView{
    UIView *averageBackView = [UIView new];
    UIView *totalBackView = [UIView new];
    
    [backView addSubview:averageBackView];
    [backView addSubview:totalBackView];
    
    [averageBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(backView);
    }];
    [totalBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(backView);
        make.left.mas_greaterThanOrEqualTo(averageBackView.right).offset(10);
    }];
    
    [LabelUtil loadLabelWithTitle:@"均价" withUnit:@"元/㎡" withLabel:self.averageLabel andBackView:averageBackView];
    [LabelUtil loadLabelWithTitle:@"约" withUnit:@"万" withLabel:self.totalLabel andBackView:totalBackView];
}

- (void)loadLocationViewWithBackView:(UIView *)backView{
    UILabel *locationTitleLabel = [UILabel new];
    locationTitleLabel.font = TextFont14;
    locationTitleLabel.textColor = TextColor120;
    locationTitleLabel.text = @"户型分布：";
    [backView addSubview:locationTitleLabel];
    [backView addSubview:self.houseTypeLocationLabel];
    
    [locationTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(backView);
    }];
    [self.houseTypeLocationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(backView);
        make.left.equalTo(locationTitleLabel.mas_right);
        make.width.lessThanOrEqualTo(self).multipliedBy(0.7);
    }];
}

- (void)loadHouseTagViewWithBackView:(UIView *)backView withTagTextArray:(NSMutableArray *)tagTitles andTagColorArray:(NSMutableArray *)colors{
    
    for (UIView *subView in backView.subviews) {
        
        if (subView.tag == 1) {
            [subView removeFromSuperview];
        }
    }
    
    UILabel *lastLabel;
    
    for (int i=0; i<tagTitles.count; i++) {
        
        UILabel *tagLabel = [UILabel new];
        tagLabel.text = [NSString stringWithFormat:@" %@ ",tagTitles[i]];
        tagLabel.textColor = colors[i%colors.count];
        tagLabel.tag = 1;
        tagLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
        tagLabel.layer.masksToBounds = YES;
        tagLabel.layer.cornerRadius = 5;
        tagLabel.layer.borderWidth = 1;
        tagLabel.layer.borderColor = ((UIColor *)colors[i%colors.count]).CGColor;
        [backView addSubview:tagLabel];
        
        if (i == 0) {
            
            [tagLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(backView);
                make.centerY.equalTo(backView);
            }];
        }else if (i == (tagTitles.count-1)){
            
            [tagLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(backView);
                make.top.bottom.equalTo(lastLabel);
                make.left.equalTo(lastLabel.right).offset(10);
                make.width.equalTo(lastLabel);
            }];
        }else{
            
            [tagLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastLabel.mas_right).offset(10);
                make.top.bottom.equalTo(lastLabel);
                make.width.equalTo(lastLabel);
            }];
        }
        lastLabel = tagLabel;
    }
    
}

//加载户型简析的背景view
- (void)loadIntroductionViewWithBackView:(UIView *)backView{
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"户型简析";
    titleLabel.font = TextFont18;
    titleLabel.textColor = TextColor60;
    [backView addSubview:titleLabel];
    [backView addSubview:self.houseIntroductionLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView).offset(TenOffsetViewMargin);
        make.top.equalTo(backView).offset(FiveOffsetViewMargin);
    }];
    
    [self.houseIntroductionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView).offset(FifteenoffsetViewMargin);
        make.top.equalTo(titleLabel.mas_bottom).offset(TenOffsetViewMargin);
        make.right.equalTo(backView).offset(-FifteenoffsetViewMargin);
        make.bottom.equalTo(backView).offset(-TenOffsetViewMargin);
    }];
    
}

//加载有声杂志，无声杂志和算价按钮的背景view
- (void)loadButtonsBackViewWithView:(UIView *)backView{
    _musicHtmlButton = [self loadImageButtonWithImageAddress:@"layoutMediaMagazineIcon"];
    _htmlButton = [self loadImageButtonWithImageAddress:@"layoutMagazineIcon"];
    _calculateButton = [self loadImageButtonWithImageAddress:@"layoutCalculatePriceIcon"];
    
    [backView addSubview:_musicHtmlButton];
    [backView addSubview:_htmlButton];
    [backView addSubview:_calculateButton];
    
    [_musicHtmlButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(backView);
        make.width.equalTo(_musicHtmlButton.mas_height);
    }];
    [_htmlButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_musicHtmlButton.mas_right).offset(TenOffsetViewMargin);
        make.top.equalTo(backView);
        make.right.equalTo(backView);
        make.width.height.equalTo(_musicHtmlButton);
    }];
    [_calculateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_htmlButton);
        make.width.height.equalTo(_htmlButton);
        make.top.equalTo(_htmlButton.mas_bottom).offset(TweentyOffsetViewMargin);
        make.bottom.lessThanOrEqualTo(backView).priorityLow();
    }];
}

- (UIButton *)loadImageButtonWithImageAddress:(NSString *)address{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [button setIconWithKey:address withPlaceHolderString:DefaultNetImage1To1 forState:UIControlStateNormal];
    return button;
}

#pragma mark - Getter
- (UIImageView *)advertisementImageView{
    if (_advertisementImageView == nil) {
        _advertisementImageView = [UIImageView new];
        _advertisementImageView.backgroundColor = BackgroundColor200;
        _advertisementImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_advertisementImageView];
    }
    
    return _advertisementImageView;
}

- (UILabel *)houseLayoutLabel{
    if (_houseLayoutLabel == nil) {
        _houseLayoutLabel = [UILabel new];
        _houseLayoutLabel.font = TextFont18;
        _houseLayoutLabel.textColor = TextColor80;
    }
    return _houseLayoutLabel;
}

- (UILabel *)saleStateLabel{
    if (_saleStateLabel == nil) {
        _saleStateLabel = [UILabel new];
        _saleStateLabel.font = TextFont14;
        _saleStateLabel.textColor = TextColor120;
    }
    
    return _saleStateLabel;
}

- (UILabel *)houseAreaLabel{
    if (_houseAreaLabel == nil) {
        _houseAreaLabel = [UILabel new];
        _houseAreaLabel.font = TextFont14;
        _houseAreaLabel.textColor = TextColor120;
    }
    
    return _houseAreaLabel;
}

- (UILabel *)averageLabel{
    if (_averageLabel == nil) {
        _averageLabel = [UILabel new];
        _averageLabel.textColor = [UIColor colorWithRed:255/255.0 green:98/255.0 blue:95/255.0 alpha:1.0];
        _averageLabel.font = TextFont18;
    }
    
    return _averageLabel;
}

- (UILabel *)totalLabel{
    if (_totalLabel == nil) {
        _totalLabel = [UILabel new];
        _totalLabel.textColor = [UIColor colorWithRed:255/255.0 green:98/255.0 blue:95/255.0 alpha:1.0];
        _totalLabel.font = TextFont18;
    }
    
    return _totalLabel;
}

- (UILabel *)houseTypeLocationLabel{
    if (_houseTypeLocationLabel == nil) {
        _houseTypeLocationLabel = [UILabel new];
        _houseTypeLocationLabel.font = TextFont14;
        _houseTypeLocationLabel.textColor = TextColor120;
        _houseTypeLocationLabel.numberOfLines = 0;
    }
    
    return _houseTypeLocationLabel;
}

- (UIView *)houseTagView{
    if (_houseTagView == nil) {
        _houseTagView = [UIView new];
    }
    
    return _houseTagView;
}

- (UILabel *)houseIntroductionLabel{
    if (_houseIntroductionLabel == nil) {
        _houseIntroductionLabel = [UILabel new];
        _houseIntroductionLabel.font = TextFont14;
        _houseIntroductionLabel.textColor = TextColor120;
        _houseIntroductionLabel.numberOfLines = 0;
    }
    
    return _houseIntroductionLabel;
}

#pragma mark - Setter
- (void)setModel:(HouseTypeInfoMessageModel *)model{
    [self.advertisementImageView setImageWithURLString:model.advertisementImageAddress];
    self.houseLayoutLabel.text = model.houseLayout;
    
    if (model.isRecommend == YES) {
        self.saleStateLabel.text = @"-推荐";
    }else{
        self.saleStateLabel.text = @"";
    }
    
    self.houseAreaLabel.text = model.houseArea;
    self.averageLabel.text = model.averagePrice;
    self.totalLabel.text = model.totalPrice;
    self.houseTypeLocationLabel.text = model.houseTypeLocation;
    
    if (model.houseTagColors == nil) {
        
        NSMutableArray *colorsArray = [NSMutableArray array];
        [colorsArray addObject:[UIColor colorWithRed:0.9608 green:0.4745 blue:0.4627 alpha:1.0]];
        [colorsArray addObject:[UIColor colorWithRed:0.9333 green:0.5255 blue:0.1137 alpha:1.0]];
        [colorsArray addObject:[UIColor colorWithRed:0.1686 green:0.5216 blue:0.8275 alpha:1.0]];
        [colorsArray addObject:[UIColor colorWithRed:0.9843 green:0.2863 blue:0.298 alpha:1.0]];
        [colorsArray addObject:[UIColor colorWithRed:0.349 green:0.7882 blue:0.7765 alpha:1.0]];
        [colorsArray addObject:[UIColor colorWithRed:0.8824 green:0.1569 blue:0.1882 alpha:1.0]];
        model.houseTagColors = colorsArray;
    }
    [self loadHouseTagViewWithBackView:self.houseTagView withTagTextArray:model.houseTags andTagColorArray:model.houseTagColors];
    self.houseIntroductionLabel.text = model.houseIntroduction;
}

@end
